package  
{
	import flash.automation.AutomationAction;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	import org.flixel.*;
	/**
	 * ...
	 * @author Euan MacDougall
	 */
	public class LevelCompeteState extends FlxState
	{
		var mPlayerInfo:PlayerInfo;
		var mLevel:int;
		
		var upgradePoints:int = 3;
		var pointsText:FlxText, continueText:FlxText;
		var weightText:FlxText, moveSpeedText:FlxText;
		var weightButton:FlxButton, moveSpeedButton:FlxButton;
		var mLevelValue:Number = 0;
		var mStuffFound:Array;
		
		public function LevelCompeteState(level:int = 1, playerInfo:PlayerInfo = null, levelValue:Number = 0, stuff:Array = null) 
		{
			mLevel = level;
			
			if (playerInfo == null)
			{
				playerInfo = new PlayerInfo();
			}
			mPlayerInfo = playerInfo;
			
			mPlayerInfo.totalValue += levelValue;
			mLevelValue = levelValue;
			mStuffFound = stuff;
		}
		
		override public function create():void
		{
			FlxG.mouse.show();
			FlxG.bgColor = 0xff114488;
			
			var text:FlxText = new FlxText(0, 30, FlxG.width, "Level ");
			text.text += mLevel;
			text.text += " Complete";
			add(text);
			text.alignment = "center";
			text.size = 30;
			
			var leftTextLeft:int = 50;
			var rightTextLeft:int = FlxG.width / 2;
			var weeBitsTop:int = text.y + text.height + 20;
			text = new FlxText(leftTextLeft, weeBitsTop, FlxG.width, "Items Stolen:");
			add(text);
			text.size = 10;
			
			var itemHeight:int = weeBitsTop + text.height + 10;
			if (mStuffFound.length == 0) mStuffFound.push("-");
			for (var i:int = 0; i < mStuffFound.length; i += 2)
			{
				var itemText:FlxText = new FlxText(leftTextLeft + 30, itemHeight, FlxG.width, "");
				itemText.text += mStuffFound[i];
				if (mStuffFound.length > i + 1) 
				{
					itemText.text += " x";
					itemText.text += mStuffFound[i + 1];
				}
				add(itemText);
				itemText.size = 10;
				itemHeight += 12;
			}
			
			text = new FlxText(rightTextLeft, weeBitsTop, FlxG.width, "Item value: ");
			text.text += mLevelValue;
			add(text);
			text.size = 10;
			
			text = new FlxText(rightTextLeft, text.y + text.height + 10, FlxG.width, "Total value: ");
			text.text += mPlayerInfo.totalValue;
			add(text);
			text.size = 17;
			
			var buttonsHeight:int = itemHeight + 30;
			if (buttonsHeight < FlxG.height / 3) buttonsHeight = FlxG.height / 3;
			
			pointsText = new FlxText(50, buttonsHeight, FlxG.width);
			pointsText.size = 10;
			add(pointsText);
			buttonsHeight += 30;
			
			//*** Stat text ***//
			var buttText:FlxText = new FlxText(100, buttonsHeight, FlxG.width, "Weight Limit");
			add(buttText);
			buttText.size = 10;
			weightText = new FlxText(230, buttonsHeight, FlxG.width);
			add(weightText);
			weightText.size = 10;
			weightButton = new FlxButton(280, buttonsHeight - 3, "+10", addWeight);
			add(weightButton);
			buttonsHeight += 25;
			
			buttText = new FlxText(100, buttonsHeight, FlxG.width, "Movement Speed");
			add(buttText);
			buttText.size = 10;
			moveSpeedText = new FlxText(230, buttonsHeight, FlxG.width);
			add(moveSpeedText);
			moveSpeedText.size = 10;
			moveSpeedButton = new FlxButton(280, buttonsHeight - 3, "+5%", addsomething);
			add(moveSpeedButton);
			buttonsHeight += 25;
			
			// Continue
			continueText = new FlxText(0, FlxG.height - 50, FlxG.width, "Be pressing space bar to continue.");
			add(continueText);
			continueText.alignment = "center";
			continueText.size = 12;
			continueText.kill();
		}
		
		override public function update():void
		{
			super.update();
			
			pointsText.text = "Remaining upgrade points:  ";
			pointsText.text += upgradePoints;
			
			weightText.text = "";
			weightText.text += mPlayerInfo.maxWeight;
			moveSpeedText.text = "";
			moveSpeedText.text += mPlayerInfo.moveSpeedModifier.toPrecision(3);
			
			if (upgradePoints == 0) continueText.revive();
			
			if (FlxG.keys.justPressed("SPACE") && upgradePoints == 0)
			{
				nextLevel();
			}
		}
		
		public function nextLevel():void
		{
			FlxG.mouse.hide();
			FlxG.switchState(new PlayState(mLevel + 1, mPlayerInfo));
		}
		
		public function giveStat(type:String, value:int = 1):void
		{
			if (value == 0) return;
			if (upgradePoints < value) return;
			switch(type)
			{
				case "weight":
					mPlayerInfo.maxWeight += 10;
					break;
					
				case "speed":
					mPlayerInfo.moveSpeedModifier += 5;
					break;
					
				default:
					return;
			}
			upgradePoints -= value;
			if (upgradePoints == 0)
			{
				weightButton.kill();
				moveSpeedButton.kill();
			}
		}
		
		public function addWeight():void
		{
			giveStat("weight");
			weightButton.status = FlxButton.NORMAL;
		}
		
		public function addsomething():void
		{
			giveStat("speed");
			moveSpeedButton.status = FlxButton.NORMAL;
		}
		
	}

}